<?php

namespace App\Exceptions;

use App\Exceptions\BaseException;
use App\Exceptions\WeChatException;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        HttpException::class,
        ModelNotFoundException::class,
        BaseException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

        if($e instanceof WeChatException) {
            $msg = explode(' ', $e->getMessage());

            if(reset($msg) == 4){
                session()->forget('userId');
            }
            logger(['weChatError' => $e->getMessage()]);
            // return response()->view('error');
        }
        if(!env('APP_DEBUG')){
            if ($e instanceof BaseException){
                if(request()->ajax() || request()->wantsJson()){
                    return response()->json($e);
                }else if(!$request->is('admin/*')){
                    if($e->getCode() == 20005){
                        return redirect()->to("/device/$request->type/connect")->with(['message' => config('exceptions.device.unbind')]);
                        // return response()->view('bind', wxConfig(['status' => ["message" => $e->getMessage()]], $request));
                    }
                    if($e->getCode() == 404|| $e->getCode() == 21001){
                        dd($e->getMessage());
                    }
                    $state['deviceCode'] = deviceCode($request->type);
                    return response()->view('state', wxConfig([
                        'status' => config('exceptions.device.notConnect'),
                        'state' => $state,
                        'host' => head($request->header()['host']),
                        'type' => $request->type,
                        ], $request));
                }else if($request->is('admin/*') && $e->getCode() == 12007){
                    return response()->view('admin/exportErrorPage', ['status' => config('exceptions.user.admin.user')]);
                }
                return back()->with(['status' => ['errCode' => $e->getCode(), 'message' => $e->getMessage()]]);
            }
        }

        return parent::render($request, $e);
    }
}
